package com.basker.pisces.common;

import java.util.function.Supplier;

/**
 * 延迟加载对象的提供器
 *
 * @param <T>
 * @author hangwen
 */
public class LazyProvider<T> {

    private Supplier<T> supplier;

    private T o = null;

    public LazyProvider(Supplier<T> supplier) {
        if (supplier == null) {
            throw new NullPointerException("LazyProvider's supplier is null!");
        }
        this.supplier = supplier;
    }

    public T get() {
        if (o == null) {
            o = supplier.get();
        }

        return o;
    }

    public boolean isPresent() {
        return o != null;
    }

    public T tryGet() {
        return o;
    }

}
